<?php
    /**
    * @abstract 阿里云oss上传类
    */
    require_once('aliyun-php-sdkv2/aliyun.php');
    use \Aliyun\OSS\OSSClient;

    class Uploader
    {   

        // 允许上传文件类型 图片
        private $File_Type;

        // 允许上传文件类型 附件
        private $attachment_File_Type;

        // 阿里云 oss 云存储类库
        public $aliyun_sdk = './aliyun-php-sdkv2/aliyun.php';
        // 请填写你的阿里 oss 账号
        public $aliyun_AccessKeyId = 'LTAIq3qaTyAP0X6l'; //LTAIW6eCyP6g8rcX
        public $aliyun_AccessKeySecret = '7Oxn8DY43ErGwed0HJ1bTXtrGcYLRD'; //SFKT49Dnsn0srRNpTpPMNyHX9VOT4M

        public $aliyun_bucket = 'lingxifu';
        public $aliyun_weburl = 'http://lingxifu.oss-cn-hangzhou.aliyuncs.com/';
        
        function __construct($options = array())
        {      
            $this->File_Type = array('jpg','jpeg','png','gif','webp');  
            $this->attachment_File_Type = array('jpg','jpeg','png','gif','webp','txt','doc','pdf','rar','xls','zip','docx','xlsx','wps','xls');             
        }

          // 上传至阿里云 附件
        public function upload_aliyunFile($name,$path){
            $info = $this->path_info($name);
            if($this->file_check_File_Type($info['extension'])){

                // require_once($this->aliyun_sdk);


                $client = OSSClient::factory(array(
                    'AccessKeyId' => $this->aliyun_AccessKeyId,
                    'AccessKeySecret' => $this->aliyun_AccessKeySecret,
                    'Endpoint' => 'http://oss-cn-hangzhou.aliyuncs.com',
                ));

                // $client->createBucket(array(
                //     'Bucket' => $this->aliyun_bucket,
                // ));

                // 取名
                $filename = md5(time().$info['filename'].mt_rand(1,20000)).'.'.$info['extension'];

                $res = $client->putObject(array(
                    'Bucket' => $this->aliyun_bucket,
                    'Key' => $filename,
                    'Content' => file_get_contents($path),
                ));

                $url = $this->aliyun_weburl.$filename;

                $return = array(
                    'error' => 0,
                    'type' => 'aliyun_oss',
                    'url' => $url,
                );

            }else{
                $return = array(
                    'error' => 101,
                    'type' => 'aliyun_oss',
                    'message' => 'filetype not allowed',
                );
            }


            return $return;
        }


        // 上传至阿里云 图片
        public function upload_aliyun($name,$path){
            $info = $this->path_info($name);
            if($this->check_File_Type($info['extension'])){

                // require_once($this->aliyun_sdk);


                $client = OSSClient::factory(array(
                    'AccessKeyId' => $this->aliyun_AccessKeyId,
                    'AccessKeySecret' => $this->aliyun_AccessKeySecret,
                    'Endpoint' => 'http://oss-cn-hangzhou.aliyuncs.com',
                ));

                // $client->createBucket(array(
                //     'Bucket' => $this->aliyun_bucket,
                // ));

                // 取名
                $filename = md5(time().$info['filename'].mt_rand(1,20000)).'.'.$info['extension'];

                $res = $client->putObject(array(
                    'Bucket' => $this->aliyun_bucket,
                    'Key' => $filename,
                    'Content' => file_get_contents($path),
                    'Content-Type' => 'image/jpeg',
                ));

                $url = $this->aliyun_weburl.$filename;

                $return = array(
                    'error' => 0,
                    'type' => 'aliyun_oss',
                    'url' => $url,
                );

            }else{
                $return = array(
                    'error' => 101,
                    'type' => 'aliyun_oss',
                    'message' => 'filetype not allowed',
                );
            }


            return $return;
        }



        // 文件命名 上传图片
        // 检查文件类型是否合法
        public function check_File_Type($extension){
            return in_array($extension, $this->File_Type);
        }

         // 文件命名 上传附件
        // 检查文件类型是否合法
        public function file_check_File_Type($extension){
            return in_array($extension, $this->attachment_File_Type);
        }


        // pathinfo替代函数,完美支持中文
        public function path_info($filepath)
        {   
            $path_parts = array();   
            $path_parts ['dirname'] = rtrim(substr($filepath, 0, strrpos($filepath, '/')),"/")."/";   
            $path_parts ['basename'] = ltrim(substr($filepath, strrpos($filepath, '/')),"/");   
            $path_parts ['extension'] = substr(strrchr($filepath, '.'), 1);   
            $path_parts ['filename'] = ltrim(substr($path_parts ['basename'], 0, strrpos($path_parts ['basename'], '.')),"/");   
            return $path_parts;
        } 


        // 返回格式 
        // CKDeitor返回
        public function ckeditor($url,$message){
            $FuncNum = $_GET['CKEditorFuncNum'];
            $str = "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction(%s,'%s','%s')</script>";
            return sprintf($str,$FuncNum,$url,$message);
        }

        // 备用文件，CKEditor
        /*
        array(1) {
          ["upload"]=>
              array(5) {
                ["name"]=>
                string(7) "cat.gif"
                ["type"]=>
                string(9) "image/gif"
                ["tmp_name"]=>
                string(14) "/tmp/phpTd6vmW"
                ["error"]=>
                int(0)
                ["size"]=>
                int(47622)
              }
            }
        */

    }

?>